package com.autotest.nore.controller;

import com.autotest.nore.common.exception.TokenEmptyException;
import com.autotest.nore.common.utils.DebugResponseConverterUtils;
import com.autotest.nore.domain.ReportCollectionCaseApi;
import com.autotest.nore.request.RunRequest;
import com.autotest.nore.response.DebugRunResponse;
import com.autotest.nore.service.ReportService;
import com.autotest.nore.service.RunService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("")
public class RunController {

    @Autowired
    private RunService runService;
    @Autowired
    private ReportService reportService;

    @PostMapping("/autotest/run/plan")
    public String runPlan(@RequestBody RunRequest runRequest, HttpServletRequest request) {
        HttpSession session = request.getSession(false); // 获取会话，如果不存在则返回null
        if (session == null || session.getAttribute("userName") == null) {
            throw new TokenEmptyException("用户未登录，请重新登录");
        }
        String userName = session.getAttribute("userName").toString();
        runRequest.setRunUser(userName);
        String reportId = UUID.randomUUID().toString();
        return runService.runPlan(runRequest,reportId);
    }

    @PostMapping("/autotest/run")
    public String run(@RequestBody RunRequest runRequest, HttpServletRequest request) {
        HttpSession session = request.getSession(false); // 获取会话，如果不存在则返回null
        if (session == null || session.getAttribute("userName") == null) {
            throw new TokenEmptyException("用户未登录，请重新登录");
        }
        String userName = session.getAttribute("userName").toString();
        runRequest.setRunUser(userName);
        String reportId = UUID.randomUUID().toString();
        return runService.run(runRequest,reportId);
    }

    @PostMapping("/autotest/debug/run")
    public String debugRun(@RequestBody RunRequest runRequest, HttpServletRequest request) {
        HttpSession session = request.getSession(false); // 获取会话，如果不存在则返回null
        if (session == null || session.getAttribute("userName") == null) {
            throw new TokenEmptyException("用户未登录，请重新登录");
        }
        String userName = session.getAttribute("userName").toString();
        runRequest.setRunUser(userName);
        return runService.debugRun(runRequest);
    }
    @GetMapping("/autotest/debug/run/caseapireport/{reportCollectionCaseId}")
    public DebugRunResponse caseApiReport(@PathVariable String reportCollectionCaseId){
        ReportCollectionCaseApi report=reportService.getReport(reportCollectionCaseId);
        System.out.println(report.getExecLog().toString());
        DebugRunResponse response=DebugResponseConverterUtils.convertToDebugRunResponse(report);
        return response;
    }
    @PostMapping("/autotest/debugCase/run")
    public String debugRunCase(@RequestBody RunRequest runRequest, HttpServletRequest request) {
        HttpSession session = request.getSession(false); // 获取会话，如果不存在则返回null
        if (session == null || session.getAttribute("userName") == null) {
            throw new TokenEmptyException("用户未登录，请重新登录");
        }
        String userName = session.getAttribute("userName").toString();
        runRequest.setRunUser(userName);
        return runService.debugRunCase(runRequest);
    }

    @GetMapping("/autotest/debug/run/casereport/{reportCollectionId}")
    public List<DebugRunResponse> caseReport(@PathVariable String reportCollectionId){
        List<ReportCollectionCaseApi> reports= reportService.getCaseReport(reportCollectionId);
        // 创建一个空的 DebugRunResponse 列表
        List<DebugRunResponse> responses = new ArrayList<>();

        // 遍历每个 ReportCollectionCaseApi 对象，并将其转换为 DebugRunResponse
        for (ReportCollectionCaseApi report : reports) {
            DebugRunResponse response = DebugResponseConverterUtils.convertToDebugRunResponse(report);
            responses.add(response);
        }

        // 返回转换后的 DebugRunResponse 列表
        return responses;
    }


    @PostMapping("/autotest/run/async")
    public String runAsync(@RequestBody RunRequest runRequest) {
        String userName = "async";
        runRequest.setRunUser(userName);
        String reportId = UUID.randomUUID().toString();

        // 异步执行任务
        runService.runAsync(runRequest,reportId);

        return reportId;
    }

    @GetMapping("/autotest/run/async/status/{reportId}")
    public String getReportStatus(@PathVariable String reportId){
        return runService.getReportStatus(reportId);
    }

}
